#include <algorithm>
#include <iostream>
#include <istream>

using namespace std;

const int MAXN = 250;
int n;
int a[MAXN];
int dp[MAXN][MAXN];

int main() {
    iostream::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        dp[i][i] = a[i];
    }
    int ans = 0;
    for (int len = 2; len <= n; len++) {
        for (int i = 1; i + len - 1 <= n; i++) {
            int j = i + len - 1;
            for (int k = i; k < j; k++) {
                if (dp[i][k] != 0 && dp[k + 1][j] != 0 && dp[i][k] == dp[k + 1][j]) {
                    dp[i][j] = max(dp[i][j], dp[i][k] + 1);
                }
            }
            ans = max(ans, dp[i][j]);
        }
    }

    cout << ans << endl;
}
